package com.sywb.chuangyebao.view.fragment;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.VideoContract;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;

/**
 * 视频列表
 */
public class VideoFragment extends BaseRefreshFragment<VideoContract.Presenter> implements VideoContract.View {

    private int fragmentWhereTab;// 当前Fragment所属的选项卡
    private int uid;// 用户ID

    private boolean isVisibleToTab = true;// 当前Fragemnt所在的Tab页是否可见
    private boolean isVisibleToUser = false;// 当前Fragemnt是否对用户可见

    /**
     * Fragment 实例
     */
    public static VideoFragment newInstance(Object... pramars) {
        VideoFragment fragment = new VideoFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_video;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        if (bundle != null) {
            fragmentWhereTab = bundle.getInt("p0", 0);
            uid = bundle.getInt("p1", -1);
        } else {
            fragmentWhereTab = getArguments().getInt("p0", 0);
            uid = getArguments().getInt("p1", -1);
        }
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(ConnectivityManager.CONNECTIVITY_ACTION)})
    public void netwotkChange(String data) {
        Logger.e("收到网络改变广播");
        if (mPresenter != null) {
            mPresenter.inspectPlayer();
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.MAINTABCHANGE)})
    public void rxMainTabChange(String data) {
        Logger.e("rxMainTabChange：" + data);

        if (Integer.parseInt(data) == fragmentWhereTab) {
            if (!isVisibleToTab) {// 之前所属Tab未显示
                isVisibleToTab = true;
                if (mPresenter != null) {
                    mPresenter.autoResume();
                }
            }
        } else {
            if (isVisibleToTab) {// 之前所属Tab未显示
                isVisibleToTab = false;
                if (mPresenter != null) {
                    mPresenter.autoPause();
                }
            }
        }
    }

    /**
     * 横竖屏切换
     *
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (isVisibleToTab && isVisibleToUser) {
            mPresenter.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Logger.e("setUserVisibleHint：" + isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (mPresenter != null) {
            mPresenter.setUserVisibleHint();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && mPresenter != null && data != null) {
            mPresenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("p0", fragmentWhereTab);
        outState.putInt("p1", uid);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onStart() {
        Logger.e("onStart");
        super.onStart();
    }

    /**
     * Activity 重新进入前台 播放逻辑
     */
    @Override
    public void onResume() {
        Logger.e("onResume");
        if (mPresenter != null) {
            mPresenter.autoResume();
        }
        super.onResume();
    }

    /**
     * Activity 不在前台时 暂停播放
     */
    @Override
    public void onPause() {
        Logger.e("onPause");
        if (mPresenter != null) {
            mPresenter.autoPause();
        }
        super.onPause();
    }

    @Override
    public void onStop() {
        Logger.e("onStop");
        super.onStop();
    }

    @Override
    public void onDestroy() {
        Logger.e("onDestroy");
        super.onDestroy();
    }

    @Override
    public String getStatisticsIdentity() {
        return uid < 0 ? "视频" : "视频（创业万里行）";
    }

    @Override
    public int getUId() {
        return uid;
    }

    @Override
    public boolean getVisibleToTab() {
        return isVisibleToTab;
    }

    @Override
    public boolean getVisibleToUser() {
        return isVisibleToUser;
    }

//    /**
//     * 进入全屏
//     */
//    @Override
//    public void startFullScreen() {
//        if (mVideoController == null) return;
//        Activity activity = WindowUtil.scanForActivity(mVideoController.getContext());
//        if (activity == null) return;
//        if (isFullScreen) return;
//        WindowUtil.hideSystemBar(mVideoController.getContext());
//        this.removeView(playerContainer);
//        ViewGroup contentView = activity
//                .findViewById(android.R.id.content);
//        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.MATCH_PARENT);
//        contentView.addView(playerContainer, params);
//        orientationEventListener.enable();
//        isFullScreen = true;
//        setPlayerState(PLAYER_FULL_SCREEN);
//    }
//
//    /**
//     * 退出全屏
//     */
//    @Override
//    public void stopFullScreen() {
//        if (mVideoController == null) return;
//        Activity activity = WindowUtil.scanForActivity(mVideoController.getContext());
//        if (activity == null) return;
//        if (!isFullScreen) return;
//        if (!mPlayerConfig.mAutoRotate) orientationEventListener.disable();
//        WindowUtil.showSystemBar(mVideoController.getContext());
//        ViewGroup contentView = activity
//                .findViewById(android.R.id.content);
//        contentView.removeView(playerContainer);
//        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.MATCH_PARENT);
//        this.addView(playerContainer, params);
//        isFullScreen = false;
//        setPlayerState(PLAYER_NORMAL);
//    }

}
